package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectTypeEnum;
import com.jingdianjichi.subject.domain.convert.SubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectBrief;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectBriefService;
import com.jingdianjichi.subject.infra.basic.service.SubjectJudgeService;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class JudgeSubjectTypeHandler implements SubjectTypeHandler{


    @Resource
    private SubjectJudgeService subjectJudgeService;
    @Override
    public SubjectTypeEnum getSubjectType() {
        return SubjectTypeEnum.getByCode(SubjectTypeEnum.JUDGE.getCode());
    }

    @Override
    public boolean add(SubjectInfoBO subjectInfoBO) {
        List<SubjectAnswerBO> subjectAnswerDTOList = subjectInfoBO.getSubjectAnswerDTOList();
        //bo To  entity
        List<SubjectJudge> subjectJudgeList = new ArrayList<>();
        subjectAnswerDTOList.forEach(subjectAnswerDTO -> {
            SubjectJudge subjectJudge = new SubjectJudge();
            BeanUtils.copyProperties(subjectAnswerDTO,subjectJudge);
            subjectJudgeList.add(subjectJudge);
        });
        boolean flag = subjectJudgeService.saveBatch(subjectJudgeList);
        return flag;
    }

    @Override
    public List<SubjectAnswerBO> query(Long subjectId) {
        List<SubjectJudge> subjectJudgeList = subjectJudgeService.queryBySubjectId(subjectId);
        ArrayList<SubjectAnswerBO> subjectAnswerDTOList = new ArrayList<>();
        if(subjectJudgeList != null && subjectJudgeList.size() > 0){
            subjectJudgeList.forEach(subjectJudge -> {
                SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
                BeanUtils.copyProperties(subjectJudge,subjectAnswerBO);
                subjectAnswerDTOList.add(subjectAnswerBO);
            });
            return subjectAnswerDTOList;
        }
        return Collections.emptyList();
    }
}
